package com.iteaj.iboot.plugin.crawler;

import com.iteaj.framework.ProfilesInclude;
import com.iteaj.framework.plugin.Plugin;
import com.iteaj.framework.security.OrderFilterChainDefinition;
import com.iteaj.framework.spi.admin.Module;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@EnableWebSocket
@CrossOrigin(originPatterns = {"*"})
@ProfilesInclude("crawler")
@ComponentScan({"com.iteaj.iboot.plugin.crawler"})
@MapperScan({"com.iteaj.iboot.plugin.crawler.mapper"})
@EnableConfigurationProperties(CrawlerProperties.class)
public class CrawlerAutoConfiguration implements Plugin, ServletContextInitializer {

    @Bean
    public Module crwModule() {
        return Module.module("crw", "网络爬虫", 10000);
    }

    @Bean
    public OrderFilterChainDefinition crawlerFilterChainDefinition() {
        return new OrderFilterChainDefinition()
                .addAnon("/crawler/**")
                .addAnon("/data/**");
    }

    @Override
    public String getPluginName() {
        return "crawler";
    }

    @Override
    public String getPluginDesc() {
        return "网络爬虫";
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("org.apache.tomcat.websocket.textBufferSize","1024000");
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}
